package com.example.a14110100420.yuejian.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a14110100420.yuejian.JaveBean.User;
import com.example.a14110100420.yuejian.Model.UserModel;
import com.example.a14110100420.yuejian.R;
import com.example.a14110100420.yuejian.Wbase.BaseActivity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class LoginActivity extends BaseActivity {
    private EditText aname, password;
    private Button login;
    private CheckBox checkbox;
    private TextView register;
    private ImageView wechat, qq, weibo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);
        initViews();
        initEvents();
    }

    public void initViews() {
        aname = (EditText) findViewById(R.id.name);
        password = (EditText) findViewById(R.id.password);
        login = (Button) findViewById(R.id.login);
        checkbox = (CheckBox) findViewById(R.id.checkbox);
        register = (TextView) findViewById(R.id.register);
        wechat = (ImageView) findViewById(R.id.wechat);
        qq = (ImageView) findViewById(R.id.qq);
        weibo = (ImageView) findViewById(R.id.weibo);

    }

    public void initEvents() {
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent, 1);
            }
        });
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = aname.getText().toString();
                String pwd = password.getText().toString();
                UserModel userModel = new UserModel();
                Call<User> call = userModel.login(name,pwd);
                //3. 发送请求
                call.enqueue(new Callback<User>(){



                    @Override
                    public void onResponse(Call<User> user, Response<User> response) {
                        String name = aname.getText().toString();
                        String pwd = password.getText().toString();
                        String userid = response.body().getId().trim();
                        String username = response.body().getUser_name().trim();

                        Log.v("获取id",userid);
                        if(userid.equals("0")){
                            Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }else{
                            if(name.equals("") || pwd.equals("")){
                                Toast.makeText(LoginActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
                            }
                            else {
                                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                saveUser(userid);
                                setUserid(userid);
                                setUsername(username);

                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);                                //传用户Id
                                startActivity(intent);
                                finish();
                            }
                        }
                    }
                    @Override
                    public void onFailure(Call<User> call, Throwable t) {
                        Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }


    public void saveUser(String user_id){
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
        editor.putString("username",aname.getText().toString());
        int userid = Integer.parseInt(user_id);
        editor.putInt("user_id",userid);

        editor.putString("userpass",password.getText().toString());
        editor.commit();//提交修改
    }

/*    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1 && resultCode==RESULT_OK){
            String username = data.getStringExtra("username");
            aname.setText(username);
        }*/
 /*       Tencent.onActivityResultData(requestCode, resultCode, data, new BaseUiListener());
        if(requestCode == SyncStateContract.Constants.REQUEST_API) {
            if(resultCode == SyncStateContract.Constants.REQUEST_LOGIN) {
                Tencent.handleResultData(data, new BaseUiListener());
            }
        }*/
    }





